package com.tao.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tao.constant.MessageConstant;
import com.tao.constant.StatusConstant;
import com.tao.context.UserContext;
import com.tao.exception.DeletionNotAllowedException;
import com.tao.mapper.CategoryMapper;
import com.tao.mapper.ProductMapper;
import com.tao.pojo.dto.CategoryDto;
import com.tao.pojo.dto.PageSelectCategoryDto;
import com.tao.pojo.entity.Category;
import com.tao.pojo.vo.CategoryVo;
import com.tao.result.PageResult;
import com.tao.result.Result;
import com.tao.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class CategoryService1 implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    ProductMapper productMapper;

    @Override
    public Category selectCategoryById(Integer id) {
        log.info("查询到分类:{}信息",id);
        Category category=categoryMapper.selectCategoryById(id);
        log.info("查询到的分类信息为:{}",category);
        return category;
    }

    @Override
    public void addCategoryService(CategoryDto categoryDto) {
        log.info("addCategoryService接收到Controller数据categoryDto:{}",categoryDto);
        Category category = new Category(categoryDto.getId(),categoryDto.getType(), categoryDto.getName(), categoryDto.getSort(), 1, LocalDateTime.now(),LocalDateTime.now(), UserContext.getThreadUserId(),UserContext.getThreadUserId());
        log.info("成功创建对象category:{}",category);
        categoryMapper.addCategoryMapper(category);
        log.info("添加成功");
    }

    @Override
    public void deleteCategoryService(Integer id) {
        log.info("deleteCategoryService接收到Controller参数id:{}",id);
        if(productMapper.selectCountByCategoryIdMapper(id)>0)
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);

        //检查是否关联套参


        categoryMapper.deleteCategoryMapper(id);
        log.info("删除成功");
    }

    @Override
    public void updateCategoryService(CategoryDto categoryDto) {
        log.info("updateCategoryService接收到参数categoryDto：{}",categoryDto);
        Category category=new Category();
        BeanUtils.copyProperties(categoryDto,category);
        category.setStatus(StatusConstant.ENABLE);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(UserContext.getThreadUserId());
        category.setUpdateUser(UserContext.getThreadUserId());
        log.info("即将修改category为:{}",category);
        categoryMapper.updateCategoryMapper(category);
        log.info("修改完成");
    }

    @Override
    public PageResult pageSelectCategoryService(PageSelectCategoryDto pageSelectCategoryDto) {
        PageHelper.startPage(pageSelectCategoryDto.getPage(), pageSelectCategoryDto.getPageSize());
        Page<CategoryVo> categoryPage = categoryMapper.pageSelectCategoryMapper(pageSelectCategoryDto);
        PageResult pageResult = new PageResult(categoryPage.getTotal(),categoryPage.getResult());
        return pageResult;

    }

    @Override
    public List<Category> selectCategoryByTypeService(Integer type) {
        log.info("查找类型：{}",type);
        List<Category> categories=categoryMapper.selectCategoryByType(type);
        log.info("查找结果categories：{}",categories);
        return categories;
    }

    @Override
    public void updateStatusService(Integer status, Integer id) {
        log.info("修改分类{}为状态:{}",id,status);
        Category category = new Category();
        category.setId(id);
        category.setStatus(status);
        log.info("修改对象为:{}",category);
        categoryMapper.updateCategoryMapper(category);
        log.info("修改成功");
    }
}
